package Utils;


import java.util.HashMap;

public class Unicode2NCRDecimal {
	HashMap<String, String> h = new HashMap<String, String>();
	public Unicode2NCRDecimal(){
		
		//0x00e1, 0x00e0, 0x1ea3, 0x00e3, 0x1ea1,
		h.put("97-769", "\u00e1");h.put("97-768", "\u00e0");h.put("97-777", "\u1ea3");h.put("97-771", "\u00e3");h.put("97-803", "\u1ea1");
		//0x00c1, 0x00c0, 0x1ea2, 0x00c3, 0x1ea0, // A
		h.put("65-769", "\u00c1");h.put("65-768", "\u00c0");h.put("65-777", "\u1ea2");h.put("65-771", "\u00c3");h.put("65-803", "\u1ea0");
		
		//0x00e2, 0x1ea5, 0x1ea7, 0x1ea9, 0x1eab, 0x1ead, // a^
		h.put("226", "\u00e2");h.put("226-769", "\u1ea5");h.put("226-768", "\u1ea7");h.put("226-777", "\u1ea9");h.put("226-771", "\u1eab");h.put("226-803", "\u1ead");
		//0x00c2, 0x1ea4, 0x1ea6, 0x1ea8, 0x1eaa, 0x1eac, // A^
		h.put("194", "\u00c2");h.put("194-769", "\u1ea4");h.put("194-768", "\u1ea6");h.put("194-777", "\u1ea8");h.put("194-771", "\u1eaa");h.put("194-803", "\u1eac");		
		
		//0x0103, 0x1eaf, 0x1eb1, 0x1eb3, 0x1eb5, 0x1eb7, // a(
		h.put("259", "\u0103");h.put("259-769", "\u1eaf");h.put("259-768", "\u1eb1");h.put("259-777", "\u1eb3");h.put("259-771", "\u1eb5");h.put("259-803", "\u1eb7");
		//0x0102, 0x1eae, 0x1eb0, 0x1eb2, 0x1eb4, 0x1eb6, // A(
		h.put("258", "\u0102");h.put("258-769", "\u1eae");h.put("258-768", "\u1eb0");h.put("258-777", "\u1eb2");h.put("258-771", "\u1eb4");h.put("258-803", "\u1eb6");
		
		//	0x0111, // dd
		//	0x0110, // DD

		h.put("273", "\u0111");
		h.put("272", "\u0110");
		
		//0x00e9, 0x00e8, 0x1ebb, 0x1ebd, 0x1eb9, // e
		h.put("101-769", "\u00e9");h.put("101-768", "\u00e8");h.put("101-777", "\u1ebb");h.put("101-771", "\u1ebd");h.put("101-803", "\u1eb9");		
		//0x00c9, 0x00c8, 0x1eba, 0x1ebc, 0x1eb8, // E
		h.put("69-769", "\u00c9");h.put("69-768", "\u00c8");h.put("69-777", "\u1eba");h.put("69-771", "\u1ebc");h.put("69-803", "\u1eb8");

		//0x00ea, 0x1ebf, 0x1ec1, 0x1ec3, 0x1ec5, 0x1ec7, // e^
		h.put("234", "\u00ea");h.put("234-769", "\u1ebf");h.put("234-768", "\u1ec1");h.put("234-777", "\u1ec3");h.put("234-771", "\u1ec5");h.put("234-803", "\u1ec7");
		//	0x00ca, 0x1ebe, 0x1ec0, 0x1ec2, 0x1ec4, 0x1ec6, // E^
		h.put("202", "\u00ca");h.put("202-769", "\u1ebe");h.put("202-768", "\u1ec0");h.put("202-777", "\u1ec2");h.put("202-771", "\u1ec4");h.put("202-803", "\u1ec6");

		//0x00ed, 0x00ec, 0x1ec9, 0x0129, 0x1ecb, // i
		h.put("105-769", "\u00ed");h.put("105-768", "\u00ec");h.put("105-777", "\u1ec9");h.put("105-771", "\u0129");h.put("105-803", "\u1ecb");		
		//0x00cd, 0x00cc, 0x1ec8, 0x0128, 0x1eca, // I
		h.put("73-769", "\u00cd");h.put("73-768", "\u00cc");h.put("73-777", "\u1ec8");h.put("73-771", "\u0128");h.put("73-803", "\u1eca");
		
		//0x00f3, 0x00f2, 0x1ecf, 0x00f5, 0x1ecd, // o
		h.put("111-769", "\u00f3");h.put("111-768", "\u00f2");h.put("111-777", "\u1ecf");h.put("111-771", "\u00f5");h.put("111-803", "\u1ecd");
		//0x00d3, 0x00d2, 0x1ece, 0x00d5, 0x1ecc,// O
		h.put("79-769", "\u00d3");h.put("79-768", "\u00d2");h.put("79-777", "\u1ece");h.put("79-771", "\u00d5");h.put("79-803", "\u1ecc");
		
		//0x00f4, 0x1ed1, 0x1ed3, 0x1ed5, 0x1ed7, 0x1ed9, // o^
		h.put("244", "\u00f4");h.put("244-769", "\u1ed1");h.put("244-768", "\u1ed3");h.put("244-777", "\u1ed5");h.put("244-771", "\u1ed7");h.put("244-803", "\u1ed9");
		//0x00d4, 0x1ed0, 0x1ed2, 0x1ed4, 0x1ed6, 0x1ed8, // O^
		h.put("212", "\u00d4");h.put("212-769", "\u1ed0");h.put("212-768", "\u1ed2");h.put("212-777", "\u1ed4");h.put("212-771", "\u1ed6");h.put("212-803", "\u1ed8");
		
		//0x01a1, 0x1edb, 0x1edd, 0x1edf, 0x1ee1, 0x1ee3, // o+
		h.put("417", "\u01a1");h.put("417-769", "\u1edb");h.put("417-768", "\u1edd");h.put("417-777", "\u1edf");h.put("417-771", "\u1ee1");h.put("417-803", "\u1ee3");
		//0x01a0, 0x1eda, 0x1edc, 0x1ede, 0x1ee0, 0x1ee2, // O+
		h.put("416", "\u01a0");h.put("416-769", "\u1eda");h.put("416-768", "\u1edc");h.put("416-777", "\u1ede");h.put("416-771", "\u1ee0");h.put("416-803", "\u1ee2");
		
		//0x00fa, 0x00f9, 0x1ee7, 0x0169, 0x1ee5, // u
		h.put("117-769", "\u00fa");h.put("117-768", "\u00f9");h.put("117-777", "\u1ee7");h.put("117-771", "\u0169");h.put("117-803", "\u1ee5");
		//0x00da, 0x00d9, 0x1ee6, 0x0168, 0x1ee4, // U
		h.put("85-769", "\u00da");h.put("85-768", "\u00d9");h.put("85-777", "\u1ee6");h.put("85-771", "\u0168");h.put("85-803", "\u1ee4");
		
		//0x01b0, 0x1ee9, 0x1eeb, 0x1eed, 0x1eef, 0x1ef1, // u+
		h.put("432", "\u01b0");h.put("432-769", "\u1ee9");h.put("432-768", "\u1eeb");h.put("432-777", "\u1eed");h.put("432-771", "\u1eef");h.put("432-803", "\u1ef1");
		//0x01af, 0x1ee8, 0x1eea, 0x1eec, 0x1eee, 0x1ef0, // U+
		h.put("431", "\u01af");h.put("431-769", "\u1ee8");h.put("431-768", "\u1eea");h.put("431-777", "\u1eec");h.put("431-771", "\u1eee");h.put("431-803", "\u1ef0");
		
		//0x00fd, 0x1ef3, 0x1ef7, 0x1ef9, 0x1ef5,// y
		h.put("121-769", "\u00fd");h.put("121-768", "\u1ef3");h.put("121-777", "\u1ef7");h.put("121-771", "\u1ef9");h.put("121-803", "\u1ef5");
		//0x00dd, 0x1ef2, 0x1ef6, 0x1ef8, 0x1ef4
		h.put("89-769", "\u00dd");h.put("89-768", "\u1ef2");h.put("89-777", "\u1ef6");h.put("89-771", "\u1ef8");h.put("89-803", "\u1ef4");
	}
	public String UnicodeTH(String l){
		char[] tt = l.toCharArray();
		if(tt.length==1){
			if(h.get(Character.codePointAt(tt, 0))!=null){
				return h.get(""+Character.codePointAt(tt, 0)).toString();
			}
		}
		String res="";
		int i=0;
		while(i<l.length()-1){
			int fc = Character.codePointAt(tt, i);
			int lc = Character.codePointAt(tt, i+1);
			Object k = h.get(fc+"-"+lc);
			if(k==null){
				if(h.get(""+fc)==null){
					res+= tt[i];
				}
				else{
					res+=h.get(""+fc).toString();
				}
				i++;
			}
			else{
				res+=k.toString();
				i=i+2;
			}
		}
		if(i==l.length()-1){
			res+=tt[i];
		}
		return res;
	}
	
	
	
	
	static final int Unicode[] = { 
		  0x00e1, 0x00e0, 0x1ea3, 0x00e3, 0x1ea1, // a
			0x00c1, 0x00c0, 0x1ea2, 0x00c3, 0x1ea0, // A
			0x00e2, 0x1ea5, 0x1ea7, 0x1ea9, 0x1eab, 0x1ead, // a^
			0x00c2, 0x1ea4, 0x1ea6, 0x1ea8, 0x1eaa, 0x1eac, // A^
			0x0103, 0x1eaf, 0x1eb1, 0x1eb3, 0x1eb5, 0x1eb7, // a(
			0x0102, 0x1eae, 0x1eb0, 0x1eb2, 0x1eb4, 0x1eb6, // A(
			0x0111, // dd
			0x0110, // DD
			0x00e9, 0x00e8, 0x1ebb, 0x1ebd, 0x1eb9, // e
			0x00c9, 0x00c8, 0x1eba, 0x1ebc, 0x1eb8, // E
			0x00ea, 0x1ebf, 0x1ec1, 0x1ec3, 0x1ec5, 0x1ec7, // e^
			0x00ca, 0x1ebe, 0x1ec0, 0x1ec2, 0x1ec4, 0x1ec6, // E^
			0x00ed, 0x00ec, 0x1ec9, 0x0129, 0x1ecb, // i
			0x00cd, 0x00cc, 0x1ec8, 0x0128, 0x1eca, // I
			0x00f3, 0x00f2, 0x1ecf, 0x00f5, 0x1ecd, // o
			0x00d3, 0x00d2, 0x1ece, 0x00d5, 0x1ecc,// O
			0x00f4, 0x1ed1, 0x1ed3, 0x1ed5, 0x1ed7, 0x1ed9, // o^
			0x00d4, 0x1ed0, 0x1ed2, 0x1ed4, 0x1ed6, 0x1ed8, // O^
			0x01a1, 0x1edb, 0x1edd, 0x1edf, 0x1ee1, 0x1ee3, // o+
			0x01a0, 0x1eda, 0x1edc, 0x1ede, 0x1ee0, 0x1ee2, // O+
			0x00fa, 0x00f9, 0x1ee7, 0x0169, 0x1ee5, // u
			0x00da, 0x00d9, 0x1ee6, 0x0168, 0x1ee4, // U
			0x01b0, 0x1ee9, 0x1eeb, 0x1eed, 0x1eef, 0x1ef1, // u+
			0x01af, 0x1ee8, 0x1eea, 0x1eec, 0x1eee, 0x1ef0, // U+
			0x00fd, 0x1ef3, 0x1ef7, 0x1ef9, 0x1ef5,// y
			0x00dd, 0x1ef2, 0x1ef6, 0x1ef8, 0x1ef4

	};

	static final String code[] = {
	// NCR Decimal
		"&#225;", "&#224;", "&#7843;", "&#227;", "&#7841;",// a
		"&#193;", "&#192;", "&#7842;", "&#195;","&#7840;",
		 "&#226;", "&#7845;", "&#7847;", "&#7849;", "&#7851;", "&#7853;",// a^
		 "&#194;","&#7844;", "&#7846;", "&#7848;", "&#7850;" ,"&#7852;",
		 "&#259;", "&#7855;", "&#7857;", "&#7859;", "&#7861;", "&#7863;",// a(
		 "&#258;", "&#7854;", "&#7856;", "&#7858;", "&#7860;", "&#7862;",
		 "&#273;",//dd
		 "&#272;",
		 "&#233;", "&#232;", "&#7867;", "&#7869;", "&#7865;",//e
		 "&#201;", "&#200;", "&#7866;","&#7868;", "&#7864;",
		 "&#234;",	"&#7871;", "&#7873;", "&#7875;", "&#7877;", "&#7879;",// e^
		 "&#202;", "&#7870;","&#7872;","&#7874;","&#7876;", "&#7878;",
		"&#237;",	"&#236;", "&#7881;", "&#297;", "&#7883;", // i
		"&#205;", "&#204;", "&#7880;", "&#296;", "&#7882;",
		"&#243;", "&#242;",	"&#7887;", "&#245;", "&#7885;", // o
		"&#211;", "&#210;", "&#7886;","&#213;", "&#7884;",
		"&#244;", "&#7889;", "&#7891;",	"&#7893;", "&#7895;", "&#7897;",// o^
		"&#212;", "&#7888;", "&#7890;", "&#7892;", "&#7894;", "&#7896;",
		 "&#417;", "&#7899;", "&#7901;","&#7903;", "&#7905;", "&#7907;",//o+
		 "&#416;", "&#7898;", "&#7900;", "&#7902;", "&#7904;", "&#7906;",
		 "&#250;", "&#249;", "&#7911;",	"&#361;", "&#7909;",//u
		 "&#218;", "&#217;", "&#7910;", "&#360;", "&#7908;",
		"&#432;", "&#7913;", "&#7915;", "&#7917;","&#7919;", "&#7921;",//u+
		"&#431;", "&#7912;","&#7914;", "&#7916;", "&#7918;", "&#7920;",
		"&#253;", "&#7923;", "&#7927;", "&#7929;",	"&#7925;", //y
		"&#221;", "&#7922;", "&#7926;", "&#7928;", "&#7924;"
	};

	public static String Convert(String t) {
		StringBuffer temp = new StringBuffer();
		int c;
		String converted;
		char[] tt = t.toCharArray();
		for (int i = 0; i < t.length(); i++) {
			c = Character.codePointAt(tt, i);
			converted = chuyendoi(c);
			if (converted.compareTo("") != 0) {
				temp.append(converted);
			} else {
				temp.append(tt[i]);
			}
		}
		return temp.toString();
	}

	private static String chuyendoi(int UNI_char) {
		for (int i = 0; i < Unicode.length; i++) {
			if (Unicode[i] == UNI_char)
				return code[i];
		}
		return "";
	}
	
}
